//树形dp - Barn Painting G
#include <iostream>
#include <vector>
using namespace std;
const int N = 1e5 + 10, MOD = 1e9 + 7;
vector<int> edges[N];
int n, k;
typedef long long ll;

ll f[N]; //f[x]表示x被涂成1号颜色, 子树有多少种方式
ll g[N]; //g[x]表示x被涂成2号颜色, 子树有多少种方式
ll h[N]; //h[x]表示x被涂成3号颜色, 子树有多少种方式

void dfs(int x, int fa)
{
    if (f[x] == 0 && g[x] == 0 && h[x] == 0)
        f[x] = g[x] = h[x] = 1;

    for (auto y : edges[x])
    {
        if (y == fa) continue;
        dfs(y, x);
        f[x] = (f[x] * (g[y] + h[y])) % MOD;
        g[x] = (g[x] * (f[y] + h[y])) % MOD;
        h[x] = (h[x] * (f[y] + g[y])) % MOD;
    }
}

int main()
{
    cin >> n >> k;
    for (int i = 1; i < n; i++)
    {
        int x, y; cin >> x >> y;
        edges[x].push_back(y);
        edges[y].push_back(x);
    }
    while (k--)
    {
        int b, c; cin >> b >> c;
        //针对已经涂色的
        if (c == 1) f[b] = 1;
        if (c == 2) g[b] = 1;
        if (c == 3) h[b] = 1;
    }
    dfs(1, 0);
    cout << ((f[1] + g[1]) % MOD + h[1]) % MOD << endl;
    return 0;
}